package com.lnj.community.quartz;

import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.scheduling.quartz.SpringBeanJobFactory;
import org.springframework.stereotype.Component;

@Component
public class CustomJobFactory extends SpringBeanJobFactory {

    // 注入 AutowireCapableBeanFactory, 用于对 Job 实例进行依赖注入
    @Autowired
    private AutowireCapableBeanFactory capableBeanFactory;

    @Override
    protected Object createJobInstance(final TriggerFiredBundle bundle) throws Exception {
        // 调用父类的方法创建 Job 实例
        final Object jobInstance = super.createJobInstance(bundle);
        // 使用 Spring 的 AutowireCapableBeanFactory 对 Job 实例进行依赖注入
        capableBeanFactory.autowireBean(jobInstance);
        return jobInstance;
    }
}
